package com.jier.controller.console;

import com.jier.common.Result;
import com.jier.common.exception.HttpParamException;
import com.jier.common.permission.Permission;
import com.jier.common.util.StringUtil;
import com.jier.controller.BaseController;
import com.jier.dto.SystemLogParamDto;
import com.jier.dto.SystemLogResultDto;
import com.jier.entities.SystemLog;
import com.jier.model.PageParam;
import com.jier.service.ISystemLogService;
import com.jier.vo.admin.SystemLogErrorMsgVo;
import com.jier.vo.admin.SystemLogVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 系统角色控制器
 *
 * @author jie_r
 * @date 2018/03/08
 */
@RestController
@RequestMapping("/console/systemLogs")
public class SystemLogController extends BaseController {
    @Autowired
    private ISystemLogService systemLogService;

    /**
     * 系统日志首页-管理页面
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/index.html", method = RequestMethod.GET)
    @ApiOperation(value = "系统日志首页-管理页面", hidden = true, httpMethod = "GET")
    @Permission(code = "010501", content = "系统日志首页-管理页面")
    public ModelAndView index() throws Exception {
        return new ModelAndView("/systemLog/systemLogs");
    }

    /**
     * 获取系统日志
     *
     * @param status
     * @param startDate
     * @param endDate
     * @param pageParam
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "", method = RequestMethod.GET, produces = "application/json;")
    @ApiOperation(value = "获取系统日志", httpMethod = "GET", produces = "application/json;")
    @Permission(code = "010501", content = "获取系统日志")
    public ResponseEntity<Result<SystemLogVo>> getSystemLogs(Integer status, String startDate, String endDate, PageParam pageParam) throws Exception {
        SystemLogParamDto param = new SystemLogParamDto();
        param.setStatus(status);
        param.setStartDate(StringUtil.isEmpty(startDate) ? startDate : startDate + " 00:00:00");
        param.setEndDate(StringUtil.isEmpty(endDate) ? endDate : endDate + " 23:59:59");
        Result<SystemLogResultDto> result = systemLogService.getSystemLogs(param, getPage(pageParam));
        List<SystemLogVo> systemLogVoList = result.getContent().stream().map(SystemLogVo::new).collect(Collectors.toList());
        return ok(new Result<>(result.getPage(), systemLogVoList));
    }

    /**
     * 获取系统日志错误信息
     *
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/{id}/error", method = RequestMethod.GET, produces = "application/json;")
    @ApiOperation(value = "获取系统日志错误信息", httpMethod = "GET", produces = "application/json;")
    public ResponseEntity<SystemLogErrorMsgVo> getSystemLogError(@PathVariable Integer id) throws Exception {
        SystemLog systemLog = systemLogService.getById(id);
        if (systemLog == null) {
            throw new HttpParamException("系统日志不存在");
        }
        return ok(new SystemLogErrorMsgVo(systemLog));
    }
}
